package application.game;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Level{
	private String name;
	private int rows;
	private int columns;
	private Cell[][] grid;
	
	public Level(String name, int rows, int columns){
		this.name = name;
		this.rows = rows;
		this.columns = columns;
		this.grid = new Cell[columns][rows];
		
		for(int row = 0; row < rows; row++){
			for(int column = 0; column < columns; column++){
				this.grid[column][row] = new Cell();
			}
		}
	}
	
	public void createCell(int column, int row, String tile, String texture, boolean hasBlock, boolean isBlockTarget, boolean isWalkable){
		this.grid[column][row] = new Cell(tile, texture, hasBlock, isBlockTarget, isWalkable);
	}
	
	public boolean save(String path){
		if(grid != null){
			try{
				path += "\\";
				path += name;
				path += ".txt";
				
				FileWriter fileWriter = new FileWriter(path);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				
				bufferedWriter.write("Level name: " + name);
				bufferedWriter.flush();
				bufferedWriter.newLine();
				bufferedWriter.flush();
				
				for(int row = 0; row < rows; row++){
					for(int column = 0; column < columns; column++){
						bufferedWriter.write("Column " + "\"column " + column + "\"");
						bufferedWriter.flush();
						
						bufferedWriter.write("Row " + "\"row " + row + "\"");
						bufferedWriter.flush();
						
						Cell currentCell = grid[column][row];
						if(currentCell != null){
							bufferedWriter.write(currentCell.getSalvableString());
							bufferedWriter.flush();
						}
						bufferedWriter.newLine();
						bufferedWriter.flush();
					}
					bufferedWriter.newLine();
					bufferedWriter.flush();
				}
				bufferedWriter.close();
			}catch(IOException e){
				e.printStackTrace();
			}
			return true;
		}
		return false;
	}
}
